﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.OleDb;

namespace GreenBuildings
{
    public partial class addBuilding : System.Web.UI.Page
    {
              
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

           //Random file name generator with jpg extension
        //    string newFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");
            string extension = ".jpg";
            string newPhotoName = Path.ChangeExtension(Path.GetRandomFileName(), extension);

            if (f_Photo.HasFile)
            //save photo into pictures folder
            { 
                f_Photo.SaveAs(Server.MapPath("pictures/" + newPhotoName)); 
                }
            
            
            string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Buildings.accdb";
            string cmdstr = "insert into Buildings(BuildingName, ShortDescription, LongDescription, Photo, Rating, Completion, Cost, Owner, BuildingType, Location, EnergyType) values(@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8,@f9,@f10,@f11)";

            OleDbConnection con = new OleDbConnection(constr);
            OleDbCommand cmd = new OleDbCommand(cmdstr, con);
            con.Open();
            cmd.Parameters.AddWithValue("@f1", tb_BuildingName.Text);
            cmd.Parameters.AddWithValue("@f2", tb_ShortDescription.Text);
            cmd.Parameters.AddWithValue("@f3", tb_LongDescription.Text);
            cmd.Parameters.AddWithValue("@f4", "pictures/" + newPhotoName);
            cmd.Parameters.AddWithValue("@f5", tb_Rating.Text);
            cmd.Parameters.AddWithValue("@f6", tb_Completion.Text);
            cmd.Parameters.AddWithValue("@f7", tb_Cost.Text);
            cmd.Parameters.AddWithValue("@f8", tb_Owner.Text);
            cmd.Parameters.AddWithValue("@f9", tb_BuildingType.Text);
            cmd.Parameters.AddWithValue("@f10", tb_Location.Text);
            cmd.Parameters.AddWithValue("@f11", tb_EnergyType.Text);
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Redirect("confirmation.aspx");
        }

        protected void rbl_Rating_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}